import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.border.*;



/**
 * ImageViewer is the main class of the image viewer application. It builds and
 * displays the application GUI and initialises all other components.
 * 
 * To start the application, create an object of this class.
 * 
 * @author Michael Kolling and David J Barnes 
 * @version 3.1
 * 
 * Changed to GUIDemo (GFJ)
 * Changed to SimulatorGUI (GFJ)
 * @version 20070907
 */
public class SimulatorGUI
{
    // static fields:
    private static final String VERSION = "Version 0.0";

    // fields:
    private JFrame               frame;
    private SimulatorViews       viewsPanel;
    private JLabel               label1;
    private JLabel               label2;
    private JButton              button1;
    private JButton              button2;
    private SimulatorModel       model;
  
    
    /**
     * Create an GUIDemo and display the GUI on screen.
     */
    public SimulatorGUI()
    {
        
        model=new SimulatorModel();
        viewsPanel = new SimulatorViews(model);
        model.setViews(viewsPanel);
        makeFrame();
        model.reset();
        
    }
    
    public static void main(String[] args){
        new SimulatorGUI();
    }
 
    /**
     * Quit function: quit the application.
     */
    private void quit()
    {
        System.exit(0);
    }


    /**
     * 'About' function: show the 'about' box.
     */
    private void showAbout()
    {
        JOptionPane.showMessageDialog(frame, 
                    "GUIDemo\n" + VERSION,
                    "About GUIDemo", 
                    JOptionPane.INFORMATION_MESSAGE);
    }

   
    
    /**
     * Enable or disable all toolbar buttons.
     * 
     * @param status  'true' to enable the buttons, 'false' to disable.
     */
    private void setButtonsEnabled(boolean status)
    {
        button1.setEnabled(status);
        button2.setEnabled(status);
    }
    
    
    /**
     * Create the Swing frame and its content.
     */
    private void makeFrame()
    {
        frame = new JFrame("Fox and Rabbit Simulation");
        JPanel contentPane = (JPanel)frame.getContentPane();
        contentPane.setBorder(new EmptyBorder(6, 6, 6, 6));

        makeMenuBar();
        
        // Specify the layout manager with nice spacing
        contentPane.setLayout(new BorderLayout(6, 6));
        
        // Create the panel in the center
        viewsPanel.setPreferredSize(new Dimension(300,300));
        viewsPanel.setBorder(new EtchedBorder());
        contentPane.add(viewsPanel, BorderLayout.CENTER);

        // Create two labels at top and bottom for the file name and status messages
        label1 = new JLabel();
        contentPane.add(label1, BorderLayout.NORTH);

        label2 = new JLabel(VERSION);
        contentPane.add(label2, BorderLayout.SOUTH);
        
        // Create the toolbar with the buttons
        JPanel toolbar = new JPanel();
        toolbar.setLayout(new GridLayout(0, 1));
        
        button1 = new JButton("button1");
        button1.addActionListener(new ActionListener() {
                               public void actionPerformed(ActionEvent e) { 
                                   System.out.println("button1"); 
                                   model.simulate(1);
                                }
                           });
        toolbar.add(button1);
        
        button2 = new JButton("button2");
        button2.addActionListener(new ActionListener() {
                               public void actionPerformed(ActionEvent e) { 
                                   System.out.println("button2"); 
                                   model.simulate(100);
                               }
                           });
        toolbar.add(button2);

        // Add toolbar into panel with flow layout for spacing
        JPanel flow = new JPanel();
        flow.add(toolbar);
        
        contentPane.add(flow, BorderLayout.WEST);
        
        // building is done - arrange the components      
        setButtonsEnabled(true);
        frame.pack();
        
        // place the frame at the center of the screen and show
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        frame.setLocation(d.width/2 - frame.getWidth()/2, d.height/2 - frame.getHeight()/2);
        frame.setVisible(true);
    }
    
    /**
     * Create the main frame's menu bar.
     * 
     */
    private void makeMenuBar()
    {
        JMenuBar menubar = new JMenuBar();
        frame.setJMenuBar(menubar);
        
        JMenu menu;
        JMenuItem item;
        
        // create the menu 1
        menu = new JMenu("Menu 1");
        menubar.add(menu);
        
        item = new JMenuItem("Item 1");
            item.addActionListener(new ActionListener() {
                               public void actionPerformed(ActionEvent e) { System.out.println("Menu 1, Item 1"); }
                           });
        menu.add(item);

        item = new JMenuItem("Item 2");
            item.addActionListener(new ActionListener() {
                               public void actionPerformed(ActionEvent e) { System.out.println("Menu 1, Item 2"); }
                           });
        menu.add(item);
        menu.addSeparator();

        item = new JMenuItem("Item 3");
            item.addActionListener(new ActionListener() {
                               public void actionPerformed(ActionEvent e) { System.out.println("Menu 1, Item 3"); }
                           });
        menu.add(item);
        
        menu.addSeparator();
        
        item = new JMenuItem("Quit");
            item.addActionListener(new ActionListener() {
                               public void actionPerformed(ActionEvent e) { quit(); }
                           });
        menu.add(item);


        // create menu 2
        menu = new JMenu("Menu 2");
        menubar.add(menu);
        
        for (int i=1; i<10; i++){
            item = new JMenuItem("Item "+i);
            item.addActionListener(new PrintAction(2,i)); //no inner class
            menu.add(item);
        }

        // create the Help menu
        menu = new JMenu("Help");
        menubar.add(menu);
        
        item = new JMenuItem("About GUIDemo...");
            item.addActionListener(new ActionListener() {
                               public void actionPerformed(ActionEvent e) { showAbout(); }
                           });
        menu.add(item);

    }
    
    private class PrintAction implements ActionListener{
        private int menuNr,itemNr;
    
        public PrintAction(int menuNr, int itemNr){
            this.menuNr=menuNr;
            this.itemNr=itemNr;
        }
        
        public void actionPerformed(ActionEvent e){
            System.out.println("Menu "+menuNr+", Item "+itemNr);
        }
    }
    
}
